package com.tiancheng.trade.order.controller.app;

import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.exception.error.BusinessErrorInfoEnum;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import com.tiancheng.trade.order.util.SecurityUtils;
import com.tiancheng.trade.order.service.QrOrderService;
import com.tiancheng.trade.order.vo.app.QrPayRecordQueryVO;
import com.tiancheng.trade.order.vo.app.QrPayVO;
import com.tiancheng.trade.order.vo.app.res.QrPayRecordQueryDetailResVO;
import com.tiancheng.trade.order.vo.app.res.QrPayRecordQueryGroupResVO;
import com.tiancheng.trade.order.vo.app.res.QrPayResVO;
import com.tiancheng.trade.order.vo.app.res.QrPayStatusQueryResVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: likailun
 * @Description: 二维码支付
 * @Date: create in 2021/9/1 4:04 下午
 */
@Slf4j
@RestController
@RequestMapping("/app/qr")
@Validated
public class QrPayAppController {

    @Resource
    private QrOrderService qrOrderService;

    /**
     * 请求二维码
     *
     * @param qrPayVO 请求二维码参数
     * @return
     */
    @PostMapping(value = "/pay", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<QrPayResVO> pay(@RequestBody @Valid QrPayVO qrPayVO, HttpServletRequest request) {
        if(StringUtils.isEmpty(qrPayVO.getUid())){
            qrPayVO.setUid(SecurityUtils.getLoginUserId());
        }
        log.info("请求获取二维码：{}", JsonUtil.toJsonString(qrPayVO));
        if(StringUtils.isEmpty(qrPayVO.getRiskInfo().getSourceIP())){
            qrPayVO.getRiskInfo().setSourceIP(request.getRemoteAddr());
        }
        BusinessErrorInfoEnum.ORDER_USER_NOT_AUTH.assertIsTrue(StringUtils.isNotEmpty(qrPayVO.getUid()));
        return qrOrderService.pay(qrPayVO);
    }

    /**
     * 查询二维码支付列表
     *
     * @param qrPayRecordQueryVO 查询参数
     * @return
     */
    @PostMapping(value = "/payRecord", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<PageInfo<QrPayRecordQueryGroupResVO>> queryPayRecord(@RequestBody @Valid QrPayRecordQueryVO qrPayRecordQueryVO) {
        if(StringUtils.isEmpty(qrPayRecordQueryVO.getUid())){
            qrPayRecordQueryVO.setUid(SecurityUtils.getLoginUserId());
        }
        BusinessErrorInfoEnum.ORDER_USER_NOT_AUTH.assertIsTrue(StringUtils.isNotEmpty(qrPayRecordQueryVO.getUid()));
        return Result.success(qrOrderService.queryPayRecord(qrPayRecordQueryVO));
    }


    /**
     * 查询二维码支付列表
     *
     * @param orderId 查询参数-订单号
     * @param uid 查询参数-用户id
     * @return
     */
    @GetMapping(value = "/queryPayStatus")
    public Result<List<QrPayStatusQueryResVO>> getPayStatus(@RequestParam("order_id") @NotEmpty String orderId,
                                                            @RequestParam(value = "uid",defaultValue = "")  String uid) {
        if(StringUtils.isEmpty(uid)){
            uid=SecurityUtils.getLoginUserId();
        }
        BusinessErrorInfoEnum.ORDER_USER_NOT_AUTH.assertIsTrue(StringUtils.isNotEmpty(uid));
        final List<QrPayStatusQueryResVO> orders = qrOrderService.getPayStatus(orderId, uid);
        return Result.success(orders);
    }

    /**
     * 查询二维码支付列表
     *
     * @param orderId 查询参数-订单号
     * @param uid 查询参数-用户id
     * @return
     */
    @GetMapping(value = "/queryOrderInfo")
    public Result<QrPayRecordQueryDetailResVO> queryOrderInfo(@RequestParam("order_id") @NotEmpty String orderId,
                                                              @RequestParam(value = "uid",defaultValue = "")  String uid) {
        if(StringUtils.isEmpty(uid)){
            uid=SecurityUtils.getLoginUserId();
        }
        BusinessErrorInfoEnum.ORDER_USER_NOT_AUTH.assertIsTrue(StringUtils.isNotEmpty(uid));
        return Result.success(qrOrderService.queryOrderDetail(orderId, uid));
    }
}
